package com.sq.partner.manager.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Calendar;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.sq.partner.manager.service.ReportService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.SqlUtil;


/**
 * 投入产出分析报表
 * SQ
 *
 */
@Controller
@RequestMapping(value = "/dataManage")
public class ReportController {
    @Resource
    private ReportService reportService;
    
    /**
     * 投入产出报表透视表
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/report")
    public String report() throws Exception {
        return "report/index";
    }
    /**
     * ie不兼容
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/report/download")
    public String reportdownload() throws Exception {
        return "report/download";
    }
    /**
     * 投入产出报表透视表
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/report/partner")
    public String reportPartner() throws Exception {
        return "report/partner";
    }
    
    
    /**
     * 查询合作伙伴名称
     * @param page
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryPtnName")
    @ResponseBody
    public List<Map<String, Object>> queryPtnName(Map<String,Object> paramMap) throws Exception {
        return reportService.queryAllPartner(paramMap);
    }
    /**
     * 查询所有机构
     * @param page
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryAllDept")
    @ResponseBody
    public List<Map<String, Object>> queryAllDept(Map<String,Object> paramMap) throws Exception {
        return reportService.queryAllDept(paramMap);
    }
    /**
     * 按合作伙伴查询保单量 总报单量 保费 总保费
     * @param page
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryAmount")
    @ResponseBody
    public Page<Map<String, Object>> queryAmount(Page<Map<String, Object>> page,String partnerCode, String depCode, String countDate, String policyType, Boolean ispolicy,Boolean isLiability,Boolean ispremium) throws Exception {
        Map<String,Object> paramMap = SqlUtil.buildPageMap(page);
        paramMap.put("partnerCode", partnerCode.isEmpty()?null:partnerCode);
        paramMap.put("depCode", depCode.isEmpty()?null:depCode);
        paramMap.put("policyType", policyType.isEmpty()?null:policyType);
        paramMap.put("countDate", countDate);
        paramMap.put("ispolicy", ispolicy);
        paramMap.put("ispremium", ispremium);
        paramMap.put("isLiability", isLiability);
        // 查询本年总量 20191121 lfd added
        String yearStartDate = countDate.substring(0, 4) + "-01-01";
        paramMap.put("yearStartDate", yearStartDate);
        page.setMap(paramMap);
        
        return reportService.queryPolicyAmount(page);
    }
    /**
     * 根据合作伙伴按机构查询单量
     * @param page
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryPartner")
    @ResponseBody
    public List<Map<String, Object>> queryPartner(String partnerCode, String countDate, String policyType,Boolean isLiability, Boolean ispolicy, Boolean ispremium) throws Exception {
        Map<String,Object> paramMap = new HashMap<String, Object>();
          paramMap.put("partnerCode", partnerCode.isEmpty()?null:partnerCode);
          paramMap.put("countDate", countDate);
          paramMap.put("ispolicy", ispolicy);
          paramMap.put("ispremium", ispremium);
          paramMap.put("isLiability", isLiability);
          paramMap.put("policyType", policyType.isEmpty()?null:policyType);
          String yearStartDate = countDate.substring(0, 4) + "-01-01";
          paramMap.put("yearStartDate", yearStartDate);
          return reportService.queryPartner(paramMap);
    }
    /**
     * 按合作伙伴下查询
     * @param partnerCode
     * @param countDate
     * @param ispolicy
     * @param ispremium
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/querypartnerAmount")
    @ResponseBody
    public List<Map<String, Object>> querypartnerAmount(String partnerCode, String countDate, String policyType, Boolean isLiability, Boolean ispolicy, Boolean ispremium) throws Exception {
        Map<String,Object> paramMap = new HashMap<String, Object>();
          paramMap.put("partnerCode", partnerCode);
          paramMap.put("countDate", countDate);
          paramMap.put("ispolicy", ispolicy);
          paramMap.put("ispremium", ispremium);
          paramMap.put("isLiability", isLiability);
          paramMap.put("policyType", policyType.isEmpty()?null:policyType);
          String yearStartDate = countDate.substring(0, 4) + "-01-01";
          paramMap.put("yearStartDate", yearStartDate);
          return reportService.querypartnerAmount(paramMap);
    }
    /**
     * 按合作伙伴下查询机构数据
     * @param partnerCode
     * @param countDate
     * @param ispolicy
     * @param ispremium
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryDeptPartner")
    @ResponseBody
    public List<Map<String, Object>> queryDeptPartner(String partnerCode, String countDate, String policyType, Boolean isLiability, Boolean ispolicy, Boolean ispremium) throws Exception {
        Map<String,Object> paramMap = new HashMap<String, Object>();
          paramMap.put("partnerCode", partnerCode);
          paramMap.put("countDate", countDate);
          paramMap.put("ispolicy", ispolicy);
          paramMap.put("ispremium", ispremium);
          paramMap.put("isLiability", isLiability);
          paramMap.put("policyType", policyType.isEmpty()?null:policyType);
          String yearStartDate = countDate.substring(0, 4) + "-01-01";
          paramMap.put("yearStartDate", yearStartDate);
          return reportService.queryDeptPartner(paramMap);
    }
    
    /**
     * 查询合作伙伴的投入成本
     * @param channelCode
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/queryChannelCost")
    @ResponseBody
    public List<Map<String, Object>> queryChannelCost(String channelCode) throws Exception {
        Map<String,Object> paramMap = new HashMap<String, Object>();
          paramMap.put("channelCode", channelCode);
          return reportService.queryChannelCost(paramMap);
    }
    
    /**
     * 按合作伙伴查询保单量 总报单量 保费 总保费
     * @param page
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/downloadPolicyFile")
    @ResponseBody
    public void downloadPolicyFile(HttpServletResponse response,HttpServletRequest req , String partnerCode, String depCode, String countDate, String policyType, Boolean ispolicy,Boolean isLiability,Boolean ispremium) throws Exception {
        Map<String,Object> paramMap = new HashMap<String, Object>();
        paramMap.put("partnerCode", partnerCode.isEmpty()?null:partnerCode);
        paramMap.put("depCode", depCode.isEmpty()?null:depCode);
        paramMap.put("policyType", policyType.isEmpty()?null:policyType);
        paramMap.put("countDate", countDate);
        paramMap.put("ispolicy", ispolicy);
        paramMap.put("ispremium", ispremium);
        paramMap.put("isLiability", isLiability);
        String yearStartDate = countDate.substring(0, 4) + "-01-01";
        paramMap.put("yearStartDate", yearStartDate);
        // 查询数据
        List<Map<String,Object>> list = reportService.queryReportFileData(paramMap);
        
        // 取过去一年日期
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = dateFormat.parse(countDate);
        calendar.setTime(date);
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM"); 
        String months_p = sf.format(calendar.getTime()) + " 保费额";
        String months_n = sf.format(calendar.getTime()) + " 保单量";

        for(int i = 1;i <= 11; i++){
            calendar.add(Calendar.MONTH, -1);// 月份减一
            months_p += "," + sf.format(calendar.getTime()) + " 保费额" ;
            months_n += "," + sf.format(calendar.getTime()) + " 保单量" ;

        }
        String months = months_p + "," + months_n;
        // 导出
        reportService.downloadFile(list, response, months);

    }
    // 查询合作伙伴名称和代码 lfd added 20190909
    @RequestMapping(value = "/queryPartnerInfo")
    @ResponseBody
    public Page<Map<String, Object>> queryPartnerInfo(Page<Map<String, Object>> page, String partnerCode, String partnerName) throws Exception {
        Map<String,Object> paramMap = SqlUtil.buildPageMap(page);
        paramMap.put("partnerCode", partnerCode.isEmpty()?null:partnerCode);
        paramMap.put("partnerName", partnerName.isEmpty()?null:partnerName);
        page.setMap(paramMap);
        return reportService.queryPartnerInfo(page);
    }
    
}
